//
//  TFkeyNoteView.swift
//  TalkfunSDKDemo
//
//  Created by 莫瑞权 on 2022/5/16.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

class TFkeyNoteView: UIView {
    @objc weak var delegate : TFkeyNoteViewDelegate?
   
    @objc let view = UIView()
    @objc let video = UIView()
    //占位图
    @objc let noStartImg = UIImageView()
    @objc let nameLab = UILabel()
    //名字背景颜色
    @objc let nameBackgroudView = UIView()
    @objc let nameImg = UIImageView()
    @objc let videoArr = NSMutableArray()
    @objc let audioStatus = UIImageView()
    //音频 视频 关闭了
    @objc let videoStatus = UIImageView()
    @objc let multimediaShutdown = TFMultimediaShutdown()
    //全屏点击
    @objc var isfullClick:Bool = false
    @objc public var touchesBegan:touchesBeganBlock?
    //做背景透明 奖杯
    @objc let scoreBackgroundColor = UIView()
    @objc let scoreButton = TFScoreButton()
    //放大缩小
    @objc let zoomButton = UIButton()
    //显示奖杯 YES:显示,false:隐藏
    @objc var showCup:Bool = false
  @objc func doubleTapAction()
    {
        if self.delegate != nil && (self.delegate?.responds(to: #selector(TFkeyNoteViewDelegate.objectKeyNoteDidDragged(_:))))!{
             
                   self.delegate?.objectKeyNoteDidDragged(self)
               }
    }
    @objc func dealSingleTap()

    {
        if (self.touchesBegan != nil) {
            self.touchesBegan!()
       }
    
        
        NotificationCenter.default.post(name: Notification.Name(rawValue: "keyboardWillHide"), object: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
       
        self.view.backgroundColor =  UIColor(red: 33/255.0, green: 38/255.0, blue: 46/255.0, alpha: 1)
        self.addSubview(self.view)
        

        self.video.backgroundColor = UIColor(red: 33/255.0, green: 38/255.0, blue: 46/255.0, alpha: 1)
        self.view.addSubview(self.video)
        
        self.multimediaShutdown.clipsToBounds = true
        self.multimediaShutdown.layer.cornerRadius = 4
        self.multimediaShutdown.alpha = 1.0
        self.multimediaShutdown.backgroundColor = UIColor.clear
        self.multimediaShutdown.isHidden = true
        self.view.addSubview(self.multimediaShutdown)

        //单击手势
        let singleRecognizer = self.view.clickGesture(target:self,action:  #selector(self.dealSingleTap))
        //双击手势
        let doubleRecognizer = self.view.doubleClickGesture(target:self,action:  #selector(self.doubleTapAction))
       /**
       *    双击手势确定监测失败才会触发单击手势的相应操作
       */
        singleRecognizer.require(toFail: doubleRecognizer)
        
        
        self.noStartImg.image = UIImage.init(named: "HTLive.bundle/weikaibo")
        self.noStartImg.isHidden = true
        self.view.addSubview(self.noStartImg)
        
        self.nameBackgroudView.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 0.8)
        self.nameBackgroudView.layer.cornerRadius = 3
      
        self.view.addSubview(self.nameBackgroudView)
        
        //奖励的背景
        self.view.addSubview(self.scoreBackgroundColor)

        //奖励 的 主容器
        self.scoreButton.setImage(UIImage.init(named: "HTLive.bundle/smalljiangbei"), for: .normal)
        self.scoreButton.backgroundColor = UIColor.clear
        self.view.addSubview(self.scoreButton)
        
        self.nameImg.image = UIImage.init(named: "HTLive.bundle/zhujiangren")
        self.nameImg.isHidden = true
        self.nameBackgroudView.addSubview(self.nameImg)
        
        self.nameLab.font = UIFont.PingFang(size: 11)
        self.nameLab.textColor = UIColor.white
        self.nameBackgroudView.addSubview(self.nameLab)
        
        //  关闭音量
        self.audioStatus.backgroundColor = UIColor.clear
        self.audioStatus.image = UIImage.init(named: "HTLive.bundle/audioOff1vN")
        self.nameBackgroudView.addSubview(self.audioStatus)
        
        self.zoomButton.backgroundColor = UIColor(red: 13/255.0, green: 15/255.0, blue: 18/255.0, alpha: 0.6)
        self.zoomButton.isHidden = true
        self.addSubview(self.zoomButton)
        self.zoomButton.setImage(UIImage.init(named: "HTLive.bundle/shrink1vN"), for: .normal)
        self.zoomButton.addTarget(self, action: #selector(zoomButtonClick), for: .touchUpInside)
        
    }
    @objc func zoomButtonClick()
    {
        self.doubleTapAction()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.view.frame = self.bounds

        self.video.frame = self.bounds
        self.multimediaShutdown.frame = self.video.bounds
        self.noStartImg.frame = CGRect(x: (self.view.frame.size.width-160)/2, y: (self.view.frame.size.height-160)/2, width: 160, height: 160)
        
        self.zoomButton.layer.masksToBounds = true
        self.zoomButton.layer.cornerRadius = 22
        self.zoomButton.frame = CGRect(x: self.frame.size.width-44-20, y: self.frame.size.height-44-15, width: 44, height: 44)
    }
    @objc func updateVideoFrame()
    {
        self.layoutSubviews()
        for i in 0..<self.videoArr.count {
            let model = self.videoArr[i] as! TalkfunRtcModel


                
            model.view.frame = self.video.bounds

                for i in 0..<model.view.subviews.count {
                    let vc:UIView = model.view.subviews[i]
            
                    vc.frame = model.view.bounds
                    
                }
                if (model.nickname != nil) {
                    self.updateNameFram(model.nickname, backcolor: false)
                   
                }
//            
        }
    }
    
   @objc func updetaModel(_ model:TalkfunRtcModel,lianmai:Bool)

    {
        self.video.isHidden = false
        self.layoutSubviews()
        if (lianmai) {
       
        } else {
            self.noStartImg.isHidden = true
         
        }
        var  Index:Bool = false
     
        //初始化数据
        for i in 0..<self.videoArr.count {
            let obj = self.videoArr[i] as! TalkfunRtcModel
       

            if (obj.xid == model.xid) {
                Index = true
                obj.score = model.score
                obj.audio = model.audio
                obj.drawPower = model.drawPower
                obj.video = model.video
                
            }
        }
        
        if (Index == false) {
            self.videoArr.removeAllObjects()
            self.videoArr.add(model)
          
        }

 
       
            model.view.frame = self.video.bounds
            
            self.video.subviews.forEach({$0.removeFromSuperview()})
            
            self.video.addSubview(model.view)
           
            for i in 0..<model.view.subviews.count {
                let vc:UIView = model.view.subviews[i]
        
                vc.frame = model.view.bounds
                
            }
           
        
       
        self.multimediaShutdown.frame = self.video.bounds
        var  Shutdown:Bool = false
        
        if (self.multimediaShutdown(model)) {

            self.multimediaShutdown.gl.colors = [UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor, UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor]
            self.multimediaShutdown.gl.locations = [1,1]
            
    
            if (model.role == .roleSpadmin) {
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/jiangshi")
            }else{
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/xueyuan")
            }
        

        self.multimediaShutdown.isHidden = false

        self.videoStatus.isHidden = true
        self.audioStatus.isHidden = true
            
            Shutdown = true
        }else{
            //视频关闭
            if (model.video == .closeForZHUBO || model.video == .closeForUser) {
                self.multimediaShutdown.gl.colors = [UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor, UIColor(red: 45/255.0, green: 52/255.0, blue: 59/255.0, alpha: 1.0).cgColor]
                self.multimediaShutdown.gl.locations = [1,1]
                self.multimediaShutdown.imageView.image = UIImage.init(named: "HTLive.bundle/closevideo-yinliang")
              
                self.multimediaShutdown.isHidden = false
                
                self.videoStatus.isHidden = true
                self.audioStatus.isHidden = false
                
            }
            //视频打开
            else if (model.video == .openForZHUBO || model.video == .openForUser) {
                self.videoStatus.isHidden = true
                self.multimediaShutdown.isHidden = true


            }
            
            
            
        }
        
        //音频 关闭
        if (model.audio == .closeForZHUBO || model.audio == .closeForUser) {
            if (Shutdown==false) {
                self.audioStatus.isHidden = false
            }
            
        }
        //音频打开
        else   if (model.audio == .openForZHUBO || model.audio == .openForUser) {
            self.audioStatus.isHidden = true

        }

        
        var temp:NSInteger = model.score
                    temp = temp/2
        
        let score = String.init(format: "%ld", temp)
        
        
        
        if (model.role == .roleeUser) {
            self.scoreButton.setTitle(score, for: .normal)
            let font:UIFont = UIFont.PingFang(size: 11)

            self.scoreButton.titleLabel!.font = font

            let newScore:String = "x" + score
            let attrsSize: CGSize = newScore.size(withAttributes: [NSAttributedString.Key.font: font])
                
                //19 * 0.6  j是图片宽高
                
             let scoreButtonW:CGFloat = 20*0.6+attrsSize.width
                
                self.scoreBackgroundColor.frame = CGRect(x: 0, y: 0, width: scoreButtonW+(scoreButtonW*0.4), height: 20)
          
                self.scoreBackgroundColor.backgroundColor = UIColor(red: 2/255.0, green: 11/255.0, blue: 20/255.0, alpha: 0.31)
            
                self.scoreButton.frame = self.scoreBackgroundColor.frame
                
          
            let maskPath1 = UIBezierPath.init(roundedRect: self.scoreBackgroundColor.bounds, byRoundingCorners:[UIRectCorner.topRight,UIRectCorner.bottomRight], cornerRadii: CGSize(width: self.scoreBackgroundColor.frame.size.height/2, height: self.scoreBackgroundColor.frame.size.height/2))
            
            
            
            let  maskLayer1 = CAShapeLayer()
            maskLayer1.frame = self.scoreBackgroundColor.bounds
            maskLayer1.path = maskPath1.cgPath
            
            self.scoreBackgroundColor.layer.mask = maskLayer1
            if (self.showCup) {
                self.scoreBackgroundColor.isHidden = false
                self.scoreButton.isHidden = false
            } else {
                self.scoreBackgroundColor.isHidden = true
                self.scoreButton.isHidden = true
            }
           
        } else {
            self.scoreBackgroundColor.isHidden = true
            self.scoreButton.isHidden = true
        }
        
       
        
        if (model.nickname != nil) {
            self.updateNameFram(model.nickname, backcolor: false)
          
        }
        
        if (model.video == .closeForZHUBO) {
            self.video.backgroundColor = UIColor(red: 33/255.0 , green: 38/255.0, blue: 46/255.0, alpha: 1)
            
        }
      
    }
    
  @objc func updateNameFram(_ name:String,backcolor:Bool)
    {
        self.nameLab.text = name
       
        
       let rect = TalkfunSharedTools.getRectWith(name, size: CGSize(width: 200, height: 20), fontSize:11)
        self.nameBackgroudView.frame = CGRect(x: 0, y: self.frame.size.height-20, width: rect.size.width+30, height: 20)
      
        if (backcolor) {
            self.nameBackgroudView.frame = CGRect(x: 0, y: self.frame.size.height-20, width: rect.size.width+10, height: 20)
     
            self.nameBackgroudView.backgroundColor = UIColor(red: 23/255.0, green: 145/255.0, blue: 251/255.0, alpha: 0.8)
         
            self.nameImg.isHidden = true
            self.nameImg.frame = CGRect(x: 0, y: 4, width: 1, height: 12)

            self.nameLab.frame = CGRect(x: self.nameImg.frame.maxX+4, y: 0, width: rect.size.width, height: 20)
            
        } else {
            if (self.audioStatus.isHidden) {
                self.nameBackgroudView.frame = CGRect(x: 0, y: self.frame.size.height-20, width: rect.size.width+30, height: 20)
               
            } else {
                self.nameBackgroudView.frame = CGRect(x: 0, y: self.frame.size.height-20, width: rect.size.width+30+22, height: 20)
                
            }
            
            self.nameBackgroudView.backgroundColor = UIColor(red: 34/255.0, green: 209/255.0, blue: 219/255.0, alpha: 0.8)
           
            self.nameImg.isHidden = false
            self.nameImg.frame = CGRect(x: 4, y: 4, width: 12, height: 12)
     
            self.nameLab.frame = CGRect(x: self.nameImg.frame.maxX+4, y: 0, width: rect.size.width, height: 20)
           
            self.audioStatus.frame = CGRect(x: self.nameLab.frame.maxX+5, y: 1.5, width: 17, height: 17)
            
        }
        
    }
    func multimediaShutdown(_ model:TalkfunRtcModel)->Bool
    {
        
        var  Shutdown:Bool = false
        //视频关闭
        if (model.video == .closeForZHUBO || model.video == .closeForUser) {
            
              //音频关闭
            if (model.audio == .closeForZHUBO || model.audio == .closeForUser) {
                   Shutdown = false
               }
           
        }
        
        return Shutdown;
    }
}
@objc protocol TFkeyNoteViewDelegate : NSObjectProtocol {
    //双击了
    func objectKeyNoteDidDragged(_ vc : TFkeyNoteView)

}
